package ee.subjects.frontend.controller.command;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ee.subjects.frontend.controller.EventFinder;
import ee.subjects.frontend.controller.ModeFinder;

public class CommandFactory {
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	private String mode=null;
	private String event=null;
	
	public CommandFactory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		this.request=request;
		this.response=response;	
	}
	
	public void PrepareCommandsExecution() throws ServletException, IOException{
		this.mode = this.findMode();
		this.event = this.findEvent(mode);
		
		System.out.println(mode);
		System.out.println(event);
		System.out.println("");
		
	}
	
	public void doExecution() throws ServletException, IOException{
		if(mode.equals("person")){
			PersonCommandFactory personCommandFactory = new PersonCommandFactory();
			personCommandFactory.getCommand(event).execute(request, response);
			
			
		}else if(mode.equals("something else")){
			//Vota moni muu kommand factory kasutusele
		}
	}
	
	private String findMode() throws ServletException, IOException{
		ModeFinder modeFinder = new ModeFinder(request,response);
		String mode = modeFinder.find();
		return mode;
	}
	
	private String findEvent(String mode) throws ServletException, IOException{
		EventFinder eventFinder = new EventFinder(request,response);
		String event = eventFinder.find(mode);
		return event;
	}
	
	public String getMode(){
		return this.mode;
	}
	
	public String getEvent(){
		return this.event;
	}
}
